package com.yzcrm.erp.kms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.yzcrm.common.annotation.NotNullValidate;
import com.yzcrm.common.annotation.Permissions;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.erp.admin.service.AdminFieldService;
import com.yzcrm.erp.crm.common.CrmEnum;
import com.yzcrm.erp.kms.service.KmsResourceService;
import com.yzcrm.swagger.annotation.ActionApi;
import com.yzcrm.swagger.annotation.Api;
import com.yzcrm.swagger.annotation.Param;
import com.yzcrm.swagger.annotation.Params;
import com.yzcrm.utils.R;

import java.util.List;

/**
 * @author jiang.xue
 * 资源管理
 */
@Api(remark = "知识库：资源管理")
public class KmsResourceController extends Controller{

    @Inject
    private AdminFieldService adminFieldService;

    @Inject
    private KmsResourceService resourceService;

    /**
     * 查看列表页
     */
    @Permissions({"kms:resource:index"})
    @ActionApi(summary = "资源列表数据", remark="资源列表数据")
    @Params({
            @Param(name="sceneId", remark="场景Id", dataType="int", defaultValue="", required=false),
            @Param(name="type", remark="权限标识 101：资源（resource）", dataType="int", defaultValue="", required=true)
    })
    public void queryPageList(BasePageRequest basePageRequest) {

        JSONObject jsonObject = basePageRequest.getJsonObject().fluentPut("type", CrmEnum.KMS_RESOURCE.getType());
        basePageRequest.setJsonObject(jsonObject);
        renderJson(resourceService.filterConditionAndGetPageList(basePageRequest));
    }

    /**
     * 添加或者资源
     */
    @ActionApi(summary = "添加或者修改资源", remark="添加或者修改资源")
    @Permissions({"kms:resource:save", "kms:resource:update"})
    public void saveOrUpdate(){

        JSONObject jsonObject = JSON.parseObject(getRawData());
        renderJson(resourceService.addOrUpdate(jsonObject));
    }

    /**
     * 根据id删除资源
     */
    @ActionApi(summary = "根据id删除资源", remark="根据id删除资源")
    @Params({
            @Param(name="resourceIds", remark="资源ID 多个ID,逗号分割", dataType="String", defaultValue="", required=true)
    })
    @Permissions("kms:resource:delete")
    @NotNullValidate(value = "resourceIds", message = "资源id不能为空")
    public void deleteByIds(@Para("resourceIds") String resourceIds) {
        renderJson(resourceService.deleteByIds(resourceIds));
    }

    /**
     * 根据资源id查询
     */
    @ActionApi(summary = "根据id查询资源", remark="根据id查询资源")
    @Params({
            @Param(name="resourceId", remark="资源ID", dataType="String", defaultValue="", required=true)
    })
    @Permissions("kms:resource:read")
    @NotNullValidate(value = "resourceId", message = "资源id不能为空")
    public void queryById(@Para("resourceId") Integer resourceId) {
        renderJson(R.ok().put("data", resourceService.queryById(resourceId)));
    }

    /**
     * 根据资源ID获取知识文件
     */
    @ActionApi(summary = "根据资源ID获取知识文件", remark="根据资源ID获取知识文件")
    @Params({
            @Param(name="resourceId", remark="资源Id", dataType="Integer", defaultValue="", required=true)
    })
    @Permissions("kms:resource:read")
    public void queryResourceFileList(@Para("resourceId") Integer resourceId) {
        renderJson(R.ok().put("data", resourceService.listFilesByResourceId(resourceId)));
    }

    /**
     * 获取资源类型
     */
    @ActionApi(summary = "获取资源类型", remark="获取资源类型")
    public void queryResourceCategoryOptions(){
        renderJson(resourceService.queryResourceCategoryOptions());
    }

    /**
     * 设置资源类型
     */
    @ActionApi(summary = "设置资源类型", remark="设置资源类型 参数示例：[\"打电话\", \"发短信\", \"上门拜访\"]")
    public void setResourceCategoryOptions(){
        JSONObject jsonObject = JSONObject.parseObject(getRawData());
        JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("value"));
        List<String> list = jsonArray.toJavaList(String.class);
        renderJson(resourceService.setResourceCategoryOptions(list));
    }

}
